﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;

namespace CoastalClimbing.Auth
{
    public partial class Hours : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                //txtHours.Text = DAL.GetHours();
                HandlePostback(IsPostBack);
            }
        }

        private void HandlePostback(bool isPostback)
        {
            string hours = null;

            if (ViewState["HOURS"] == null)
            {
                hours = DAL.GetHours();
                ViewState["HOURS"] = hours;
            }
            else
            {
                hours = ViewState["HOURS"].ToString();
            }

            if (isPostback)
            {
                hours = txtHours.Text;
                ViewState["HOURS"] = hours;
            }

            if (!string.IsNullOrWhiteSpace(hours))
            {
                txtHours.Text = hours;

                hours = hours.Replace("\n", "<br />");

                litHours.Text = hours;                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string hours = txtHours.Text;

            if(!string.IsNullOrWhiteSpace(hours))
            {
                try
                {
                    DAL.UpdateHours(hours, Membership.GetUser().ProviderUserKey.ToString());

                    lblMessage.Text ="Hours successfully updated.";
                }
                catch(Exception ex)
                {
                    lblMessage.Text = ex.Message;
                }
            }
        }

        protected void txtHours_TextChanged(object sender, EventArgs e)
        {
            string eArg = Request.Params["__EVENTARGUMENT"];

            if (eArg == "txtHoursKeyPress")
            {
                HandlePostback(true);
                txtHours.Focus();
            }
        }
    }
}